import 'package:flutter/material.dart';
import 'package:flutter_context_menu/flutter_context_menu.dart';
import 'package:provider/provider.dart';

import 'pages/note_page.dart';

class TaskList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TaskList();
  }

  TaskStatus status;

  TaskList({this.status = TaskStatus.New});
}

class _TaskList extends State<TaskList> {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: ListView.builder(
        itemBuilder: (c, i) {
          var titleList =
              context.read<TaskListProvider>().newTaskList.where((item) {
            return item.status == this.widget.status;
          }).toList();
          var _currentSelectedIndex =
              context.read<TaskListProvider>().currentSelectedIndex;
          return ContextMenuRegion(
            contextMenu: ContextMenu(entries: [
              const MenuHeader(text: "Context Menu"),
              MenuItem(
                  label: 'Del',
                  icon: Icons.delete,
                  onSelected: () {
                    context.read<TaskListProvider>().removeNewTaskByIndex(i);
                  })
            ], shortcuts: {
              const CharacterActivator('d', control: true): () {
                print('object');
              }
            }),
            child: ListTile(
              leading: Builder(builder: (context) {
                return Checkbox(
                    value: titleList[_currentSelectedIndex].status ==
                        TaskStatus.Del,
                    onChanged: (s) {
                      if (s ?? false) {
                        context
                            .read<TaskListProvider>()
                            .removeNewTaskByIndex(i);
                      } else {
                        context
                            .read<TaskListProvider>()
                            .removeDelTaskByIndex(i);
                      }
                    });
              }),
              subtitle: Text(
                'subtitle333333333333',
                overflow: TextOverflow.ellipsis,
                style: titleList[_currentSelectedIndex].status == TaskStatus.Del
                    ? context.read<TaskListProvider>().completeTextStyle
                    : const TextStyle(),
              ),
              title: Text(
                titleList[i].taskContent,
                style: titleList[_currentSelectedIndex].status == TaskStatus.Del
                    ? context.read<TaskListProvider>().completeTextStyle
                    : const TextStyle(),
              ),
              onTap: () {
                print(' index = $i');
                context
                    .read<ContentProvider>()
                    .update('you click ${titleList[i].toString()}');
                context.read<TaskListProvider>().selectByIndex(i);
              },
              selected: _currentSelectedIndex == i,
              selectedColor: Colors.pinkAccent,
            ),
          );
        },
        itemCount: context.watch<TaskListProvider>().newTaskList.where((item) {
          return item.status == this.widget.status;
        }).length,
      ),
    );
  }
}

enum TaskStatus { New, Doing, Del }

class Task {
  String taskContent;
  TaskStatus status;
  bool notify;

  Task({required this.taskContent, required this.status, this.notify = false});

  @override
  String toString() {
    return '{taskContent = $taskContent,status = $status,notify = $notify}';
  }
}

class TaskListProvider with ChangeNotifier {
  var _newTaskList = <Task>[];
  var _completedTaskList = <Task>[];
  var _currentSelectedIndex = 0;
  TextStyle _completeTextStyle = const TextStyle(
      decoration: TextDecoration.lineThrough,
      decorationStyle: TextDecorationStyle.solid);

  List<Task> get newTaskList => _newTaskList;

  List<Task> get completedTaskList => _completedTaskList;

  TextStyle get completeTextStyle => _completeTextStyle;

  int get currentSelectedIndex => _currentSelectedIndex;

  void addCompletedTask(Task newItem) {
    _completedTaskList.add(newItem);
    notifyListeners();
  }

  void addNewTask(Task newItem) {
    _newTaskList.add(newItem);
    notifyListeners();
  }

  void removeNewTaskByIndex(int index) {
    _newTaskList[index].status = TaskStatus.Del;
    notifyListeners();
  }

  void removeDelTaskByIndex(int index) {
    _newTaskList[index].status = TaskStatus.New;
    notifyListeners();
  }

  void selectByIndex(int index) {
    _currentSelectedIndex = index;
    notifyListeners();
  }
}
